package com.glxy.dao.impl;

import com.glxy.dao.AddressDao;
import com.glxy.pojo.Address;
import com.glxy.utils.DBUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class AddressDaoImpl implements AddressDao {
    private static final String table = "smbms_address";
    DBUtils dbUtils = DBUtils.getInstance();

    @Override
    public Integer totalCount() {
        return null;
    }

    @Override
    public ArrayList<Address> findAll() {
        String sql = "select * from "+table+";";
        return dbUtils.executeQuery(sql, null, Address.class);
    }

    @Override
    public boolean insert(Address address){
        StringBuffer sql = new StringBuffer("insert into "+table+"(");
        StringBuffer placeholder = new StringBuffer();
        ArrayList<Object> params = new ArrayList<>();

        try {
            Iterator<Field> it = Arrays.stream(Address.class.getDeclaredFields()).iterator();
            Field field = it.next();

            field.setAccessible(true);
            if (field.get(address) != null) {
                sql.append(field.getName());
                placeholder.append("?");
                params.add(field.get(address));
            }
            while (it.hasNext()) {
                field = it.next();
                field.setAccessible(true);
                if (field.get(address) != null) {
                    sql.append(", ").append(field.getName());
                    placeholder.append(", ").append("?");
                    params.add(field.get(address));
                }

            }
            sql.append(") values (").append(placeholder).append(");");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return dbUtils.execute(new String(sql), params) == 1;
    }

    @Override
    public Integer update(Address address){
        StringBuffer sql = new StringBuffer("update "+table+" set ");
        ArrayList<Object> params = new ArrayList<>();

        Iterator<Field> it = Arrays.stream(Address.class.getDeclaredFields()).iterator();


        try {
            Field field = it.next();
            field.setAccessible(true);
            Long id = (Long) field.get(address);

            field = it.next();
            field.setAccessible(true);
            if (field.get(address) != null) {
                sql.append(field.getName()).append("=?");
                params.add(field.get(address));
            }
            while (it.hasNext()) {
                field = it.next();
                field.setAccessible(true);
                if (field.get(address) != null) {
                    sql.append(", ").append(field.getName()).append("=?");
                    params.add(field.get(address));
                }
            }
            sql.append(" where id = ").append(id).append(";");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return dbUtils.execute(new String(sql), params);
    }

    @Override
    public boolean delete(Long id) {
        StringBuffer sql = new StringBuffer("delete from "+table+" where id = "+id+";");

        return dbUtils.execute(new String(sql), null) == 1;
    }
}
